package com.cn.kehong.service;


import com.cn.kehong.api.IProductReworkService;
import com.cn.kehong.conf.Config;
import com.cn.kehong.dao.*;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.*;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

@EnableScheduling
@Service
public class ProductReworkServiceImpl implements IProductReworkService {
    @Autowired
    private ProductReworkMapper productReworkMapper;
    @Autowired
    private Config config;
    @Autowired
    private ProductPackageMapper productPackageMapper;
    @Override
    public Map<String, Object> getProductReworkByApp(QueryForm queryForm) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        //进行中
        queryForm.setBackStatus("1");
        int count = productReworkMapper.getProductReworkByAppCount(queryForm);
        List<ProductRework> productReworkJxz = productReworkMapper.getProductReworkByAppJxz(queryForm);
        if (productReworkJxz.size() > 0) {
            productReworkJxz.get(0).setCount(count);
            resultMap.put("CompletedList", productReworkJxz);
        } else {
            resultMap.put("CompletedList", new ArrayList<>());
        }
        //已完成
        queryForm.setBackStatus("2");
        List<ProductRework> productReworkYwc = productReworkMapper.getProductReworkByAppJxz(queryForm);
        if (productReworkYwc.size() > 0) {
            resultMap.put("isCompletedList", productReworkYwc);
        } else {
            resultMap.put("isCompletedList", new ArrayList<>());
        }

        return resultMap;
    }



    @Override
    public String addProductRework(ProductRework productRework, Map<String, String> map) throws Exception {
        if (null == productRework) {
            map.put("message", "请输入返工记录信息");
            return "error";
        }
        String result = this.productReworkChange(productRework);
        if (!"".equals(result)) {
            map.put("message", result);
            return "error";
        }
        //生产批次，提交时候需检验是否为该厂房车间下的生产批次
        String productCode = productRework.getProductCode();
        ProductPackageExample example = new ProductPackageExample();
        example.createCriteria().andProductCodeEqualTo(productCode).andStatusEqualTo(Constants.NORMAL_STATUS);
        List<ProductPackage> productPackages = productPackageMapper.selectByExample(example);
        if (productPackages.size() < 1) {
            map.put("message", "生产批次不存在！");
            return "error";
        }

        String backStartTime = productRework.getBackStartTime();
        LocalDateTime dateTime = LocalDateTime.parse(backStartTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalTime time = dateTime.toLocalTime();
        if (time.isAfter(LocalTime.of(8, 0)) && time.isBefore(LocalTime.of(20, 0))) {
            productRework.setBancCi("A");
        } else {
            productRework.setBancCi("B");
        }
        String id = CommonUtil.generateRandomNum("productRework_");
        productRework.setId(id);
        productRework.setStatus("0");
        productRework.setCreateTime(new Date());
        int count = productReworkMapper.insertSelective(productRework);

        if (count < 1) {
            map.put("message", "新增失败！");
            return "error";
        }
        map.put("id", id);
        map.put("message", "新增成功！");
        return "success";
    }


    @Override
    public void exportProductRework(QueryForm queryForm, HttpServletResponse response) throws Exception {
        Map<String,Object> results = Maps.newHashMap();
        List<ProductRework> productReworkYwc = productReworkMapper.getProductReworkByAppJxz(queryForm);
        results.put("list", productReworkYwc);
        String fileName = CommonUtil.generateRandomNum("fg-")+".xlsx";
        String filePath = config.filePath+fileName;
        String nameStr="返工明细"+".xlsx";
        TemplateExcelUtils.downLoadExcel(filePath,nameStr,results,"fgmx.xlsx", response);
    }

    @Override
    public String updateProductRework(ProductRework productRework) throws Exception {
        String id = productRework.getId();
        if (StringUtils.isEmpty(id)) {
            return "id is null";
        }
        String result = this.productReworkChange(productRework);
        if (!"".equals(result)) {
            return result;
        }
        ProductReworkExample example = new ProductReworkExample();
        example.createCriteria().andIdEqualTo(id);
        productRework.setUpdateTime(new Date());
        int count = productReworkMapper.updateByExampleSelective(productRework, example);
        return CommonUtil.outStr(count);
    }

    @Override
    public List<ProductRework> getProductReworkList(QueryForm queryForm) throws Exception {
        int count = productReworkMapper.getProductReworkListCount(queryForm);
        if (count == 0) {
            return Lists.newArrayList();
        }
        List<ProductRework> productReworks = productReworkMapper.getProductReworkList(queryForm);
        if (productReworks.size() > 0) {
            productReworks.get(0).setCount(count);
        }
        return productReworks;
    }

    private String productReworkChange(ProductRework productRework) {

        String result = "";

            if (StringUtils.isBlank(productRework.getFactoryName())) {
                result = "请输入厂房车间";
                return result;
            }
            if (StringUtils.isBlank(productRework.getBackType())) {
                result = "请输入返工类型";
                return result;
            }
            if (StringUtils.isBlank(productRework.getDianciCode())) {
                result = "请输入电除磁罐号";
                return result;
            }
            if (StringUtils.isBlank(productRework.getProductCode())) {
                result = "请输入制程批号";
                return result;
            }

        return result;


    }

}
